package cn.yu.takeoutbackend.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketTransportRegistration;

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/ws").setAllowedOrigins("http://localhost:3000").withSockJS(); // 提供WebSocket端点，支持SockJS回退
    }

    @Override
    public void configureMessageBroker(MessageBrokerRegistry registry) {
        final ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
        taskScheduler.initialize();

        registry.enableSimpleBroker("/topic") // 启用内存消息代理，处理/topic前缀的目的地
                .setHeartbeatValue(new long[]{30*1000, 30*1000})  // 设置心跳时间间隔和超时时间
                .setTaskScheduler(taskScheduler);               //
    }

    @Override
    public void configureWebSocketTransport(WebSocketTransportRegistration  registration){
        registration.setSendTimeLimit(15*1000) // 设置发送消息超时时间为15秒
                .setSendBufferSizeLimit(512*1024) // 设置发送缓冲区大小为512KB
                .setTimeToFirstMessage(30*1000); // 设置首次消息超时时间为30秒
    }


}
